﻿/**
*   @class : class1
*   @type : UI Object class
*   @authors : djm
*   @description : paints a debug window over the main screen of a Flash movie
*   @versions :
*      r01 - class creation
*      r02 - draggable, displays debug trace
*      r03 - colors, text styles
 * @author MAC Novatek
 */
class com.novatek.util.Debugger extends MovieClip {
	// Constants:
	public static var CLASS_NAME:String = "Debugger";
	public static var LINKAGE_ID:String = "com.dev.Debugger";
	private static var instance : Debugger;
	private var container_mc:MovieClip;
	private var dragger_mc:MovieClip;

	private function doDrag() {
		this._parent.startDrag();
	}
	private function doDrop() {
		this._parent.stopDrag();
	}
	// Initialization:
	public function Debugger() {
		container_mc = _root.createEmptyMovieClip("debugger", _root.getNextHighestDepth());
		dragger_mc = container_mc.createEmptyMovieClip("dragger_mc", getNextHighestDepth());
		drawRectangle(container_mc,480,240,0xDDDDDD,100);
		drawRectangle(dragger_mc,480,24,0xDDDDDD,50);
		makeFields(container_mc);
		container_mc.dragger_mc.onPress = doDrag;
		container_mc.dragger_mc.onRelease = doDrop;
		Key.addListener (this);
		container_mc._visible = false;
	}
	private function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void {
			target_mc.clear();
			target_mc.beginFill(fillColor,fillAlpha);
			target_mc.moveTo(0,0);
			target_mc.lineTo(boxWidth,0);
			target_mc.lineTo(boxWidth,boxHeight);
			target_mc.lineTo(0,boxHeight);
			target_mc.lineTo(0,0);
			target_mc.endFill();
	}
	private function makeFields(target_mc){
			target_mc.createTextField("output_txt",1,3,24,472,211);
			target_mc.output_txt.multiline = true;
			target_mc.output_txt.wordWrap = true;
			target_mc.output_txt.border = true;
			target_mc.output_txt.background = true;
			target_mc.output_txt.selectable = true;
			var output_fmt:TextFormat = new TextFormat();
			output_fmt.font="Courier New";
			output_fmt.size= 14;
			target_mc.output_txt.setNewTextFormat(output_fmt);
			target_mc.createTextField("titleBar_txt",4,2,2,474,24);
			target_mc.titleBar_txt.multiline = false;
			target_mc.titleBar_txt.wordWrap = false;
			target_mc.titleBar_txt.border = false;
			target_mc.titleBar_txt.background = false;
			target_mc.titleBar_txt.selectable = false;
			target_mc.titleBar_txt.text = "Debugger: Press CTRL-Backspace to close."
			var title_fmt:TextFormat = new TextFormat();
			title_fmt.font="Verdana";
			title_fmt.size= 14;
			title_fmt.bold = true;
			target_mc.titleBar_txt.setTextFormat(title_fmt);
		}
	private function onKeyDown (code : Number){
		// Turn on SCORM debugger (CTRL + BACKSPACE)
		if (Key.isDown (17) && Key.getCode () == 8)
		{
			//sProxy.showDebugger ();
			showDebugger();
		}
	}
	public static function getInstance():Debugger {
		if (instance == undefined) {
			instance = new Debugger();
		}
		return instance;
	}
	public function showDebugger(){
		container_mc._visible = ! container_mc._visible;
		trace("showDebugger");
	}
	public function send(txt:String):Void {
		_root.debugger.output_txt.text += txt+"\n";
		_root.debugger.output_txt.scroll = _root.Debugger.output_txt.maxscroll;
		trace(txt);
	}

}